from PyQt5.QtCore import Qt, QModelIndex
from PyQt5.QtWidgets import QStyledItemDelegate, QLineEdit


class Text(QStyledItemDelegate):
    def __init__(self, parent, constraints):
        super().__init__(parent)
        self.constraints = constraints

    def createEditor(self, parent, option, index):
        textline = QLineEdit(parent)

        ptr = self.constraints.find("maxLength")
        if(ptr > -1):
            ptr2 = self.constraints.find(")")
            length = self.constraints[ptr+2, ptr2]

        return textline
        
    def setEditorData(self, textline, index):
        textline.setText(index.model().data(index, Qt.EditRole))


    def setModelData(self, textline, model, index):
        model.setData(index, textline.text(), Qt.EditRole)


    def updateEditorGeometry(self, textline, option, index):
        textline.setGeometry(option.rect)
